package com.imau.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/ResponseServletStream")
public class ResponseServletStream extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.读取本地文件
        FileInputStream fis = new FileInputStream("F:/wxl.jpg");

        //2.获取输出流
        ServletOutputStream out = response.getOutputStream();

        //3.输出,边读取边写
        byte[] buff = new byte[1024];
        int len = 0;

        while ((len = fis.read(buff)) != -1) {
            out.write(buff, 0, len);
        }
        fis.close();
        out.close();

    }
}
